package T2_10;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class T4 {
    public static void main(String[] args) {
        int[] ints = {0,1,0,3,12};
        moveZeroes(ints);

    }
    public static void moveZeroes(int[] nums) {
       /* int zero=0;
        for (int i=0;i<nums.length;i++){
            if (nums[i]==0)
                zero++;
        }
        //将nums转为list
        List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toList());
        //移除所有的0
        list.removeIf(s->s==0);
        for (int i=0;i<zero;i++){
            list.add(0);
        }
        int[] nums2=new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            nums2[i]= list.get(i);
        }
        System.out.println(Arrays.toString(nums2));*/
        //slow会停在值为零的位置
        int slow=0;
        for(int fast=0;fast< nums.length;fast++){
            //如果不为零就将值给slow（为0）的元素
            if(nums[fast]!=0){
                nums[slow]=nums[fast];
                slow++;
            }

        }
        //将剩余位置填充为0
        for(int i=slow;slow<nums.length;slow++){
            nums[slow]=0;
        }
    }
}
